package com.jlz.managers.user;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jlz.beans.user.LinkBean;
import com.jlz.beans.user.UserBean;
import com.jlz.managers.def.Sites;
import com.julewa.db.Identifier;
import com.julewa.db.Store;

@Service("Users")
public class Users {
	private Store<UserBean> store = new Store<UserBean>(UserBean.class);
	private @Autowired Identifier idsrv;
	private @Autowired Links links;
	//
	@SuppressWarnings("unused")
	@PostConstruct
	private void init(){
		UserBean u = get(0);
		if(u != null)return;
		u = store.create();
		u.setName("tester");
		u.setID(0l);
		store.insert(u);
		//
		LinkBean lnk = links.create();
		lnk.setUserId(u.getID());
		lnk.setSiteId(Sites.JULEZU);
		lnk.setCode("test");
		lnk.setPassword("test");
		lnk.setState(Links.State.USING.getValue());
		//
		links.append(lnk);
	}
	public UserBean create(){
		return store.create();
	}
	//
	public boolean append(UserBean user){
		user.setID(idsrv.generate("user"));
		return store.insert(user);
	}
	
	public boolean update(UserBean user){
		return store.update(user);
	}
	
	public boolean remove(UserBean user){
		return store.delete(user);
	}
	
	public UserBean get(long userid){
		return store.get(userid);
	}
	
	
}
